package com.xxx.controller;

import com.xxx.common.Code;
import com.xxx.common.Result;
import com.xxx.pojo.Employee;
import com.xxx.service.EmployeeService;
import com.xxx.util.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private EmployeeService service;

    @PostMapping("/okLogin")
    public Result okLogin(HttpServletRequest request, @RequestBody Employee employee){
        Employee e = service.okLogin(employee.getUsername(), employee.getPassword());
//        System.out.println(employee);
        Result r = new Result();
        HttpSession session = request.getSession();
        if (null == e){
            r.setCode(Code.GET_ERR);
            r.setMessage("用户名或密码错误！！！");
        }else {
            r.setCode(Code.GET_OK);
            session.setAttribute("employeeId",e.getId());
            session.setMaxInactiveInterval(60*30);
        }
        return r;
    }

    @GetMapping("/loginOut")
    public void loginOut(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.getSession().invalidate();

        response.sendRedirect("/basis/login.html");
    }
}
